import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);//vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
    state:{//存放状态
      cartList:wx.getStorageSync("cartList") || []
    },
    mutations:{
      setCartList(state,cartList){
        state.cartList = cartList
        // console.log('mu',state.cartList)
        wx.setStorageSync("cartList", cartList);
      }
    },
    getters:{
      // 选中的总价格
      totalPrice(state){
        let price = 0
        state.cartList.forEach(item=>{
          if(item.checked){
            price += item.goods_price * item.nums
          }
        })
        return price
      },
      // 选中的总数量
      totalNum(state){
        let sum = 0
        state.cartList.forEach(item=>{
          if(item.checked){
            sum += item.nums
          }
        })
        return sum
      },
      // 用户待结算的商品数组 - 勾选了的商品
      checkedCartList(state){
        return state.cartList.filter(item=>item.checked)
      }
    }
})
export default store